/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：FrontUserAddrDaoImpl.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontUserAddrDao;
import com.xteam.ecsystem.entities.BaseDistributeRegion;
import com.xteam.ecsystem.entities.BaseReceiveAddress;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端用户-收货人地址数据访问实现类
 * @author liuzhi
 *
 */
@Repository
public class FrontUserAddrDaoImpl extends DDLPrefix implements IFrontUserAddrDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public int getUserAddrNums(String braMember) {
		try {
			String hql = SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE +  "braMember = ?";
			List list = this.baseDao.find(hql, new Object[]{braMember});
			
			if (list != null && !list.isEmpty()) {
				return list.size();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<BaseReceiveAddress> getUserAddrs(String braMember) {
		try {
			String hql = SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE +  "braMember = ?";
			List<BaseReceiveAddress> list = this.baseDao.find(hql, new Object[]{braMember});
			
			if (list != null && !list.isEmpty()) {
				return list;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public String saveOrUpdateObj(BaseReceiveAddress bra, String braId) {
		try {
			if (StringUtil.isNotNullOrEmpty(braId)) {
				this.baseDao.update(bra);
				
				return braId;
			}else{
				this.baseDao.save(bra);
				return bra.getBraId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public BaseDistributeRegion getRegionInfo(String regionId){
		try {
			String hql = SQL_FROM + BaseDistributeRegion.REF + SQL_WHERE + "bdrId=?";
			List list = this.baseDao.find(hql, new Object[]{regionId});
			
			if (list != null && !list.isEmpty()) {
				BaseDistributeRegion region = (BaseDistributeRegion)list.get(0);
				return region;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public void saveCancelUserDefAddr(String braMember) {
		try {
			String hql = SQL_UPDATE + BaseReceiveAddress.REF + " set braIsdefault = 0"
					+ SQL_WHERE + "braMember = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{braMember});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void saveUserDefAddr(String braId) {
		try {
			String hql2 = SQL_UPDATE + BaseReceiveAddress.REF + " set braIsdefault = 1"
					+ SQL_WHERE + "braId = ? ";
			
			this.baseDao.executeUpdate(hql2, new Object[]{braId});
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void deleteUserAddr(String braId) {
		try {
			Object obj = this.baseDao.get(BaseReceiveAddress.class, braId);
			
			this.baseDao.delete(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<BaseDistributeRegion> getRegionList(String bdrId, String bdrParent){
		List<BaseDistributeRegion> list = new ArrayList<BaseDistributeRegion>();
		String hql = "";
		
		try {
			hql = SQL_FROM + BaseDistributeRegion.REF + SQL_WHERE + "1=1";
			
			if(StringUtil.isNotNullOrEmpty(bdrId)){
				hql += SQL_AND + "bdrId=?";
				list = this.baseDao.find(hql, new Object[]{bdrId});
			}else if(StringUtil.isNotNullOrEmpty(bdrParent)){
				hql += SQL_AND + "bdrParent=?";
				list = this.baseDao.find(hql, new Object[]{bdrParent});				
			}else{
				list = this.baseDao.find(hql);		
			}
			
			if (!list.isEmpty()) {
				return list;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	/**
	 * 
	 * 获取收货人信息
	 * @see com.xteam.ecsystem.dao.IFrontOrderDao#getReceiverInfo(java.lang.String)
	 */
	@Override
	public BaseReceiveAddress getReceiverInfo(String memberId) {
		BaseReceiveAddress bra = new BaseReceiveAddress();
		if(memberId!=null){
			String hql = SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE + " braMember = ? and braIsdefault = 1";
			bra = (BaseReceiveAddress) this.baseDao.findObject(hql, new Object[]{ memberId });
			if(bra==null){
				hql = SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE + " braMember = ? ";
				bra = (BaseReceiveAddress) this.baseDao.findObject(hql, new Object[]{ memberId });
			}
		}
		return bra;
	}
	
	/**
	 * 
	 * 有无收货地址
	 * @see com.xteam.ecsystem.dao.IFrontOrderDao#hasAddress()
	 */
	@Override
	public boolean hasAddress(String memberId) {
		String hql = SQL_SELECT + "count(*)" +SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE + " braMember = ? ";
		Long arg = this.baseDao.count(hql, new Object[]{ memberId });
		if(arg.intValue()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public BaseReceiveAddress getReceiverInfoById(String addrId){
		try {
			String hql = SQL_FROM + BaseReceiveAddress.REF + SQL_WHERE + " braId = ? ";
			BaseReceiveAddress bra = (BaseReceiveAddress) this.baseDao.findObject(hql, new Object[]{addrId});
			
			return bra;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
